/*
 * Copyright ©2017 贾维程. All rights reserved.
 */

package org.handover.controller.admin.userInformation;

import org.handover.Tools.Tools;
import org.handover.domain.User;
import org.handover.domain.UserInformation;
import org.handover.repository.UserInformationRepository;
import org.handover.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/admin/userInformation/")
public class UserInformationController {

    @Autowired
    private UserInformationRepository userInformationRepository;
    @Autowired
    private UserRepository userRepository;

    @GetMapping("edit")
    public String getEditUserInformation(Model model, HttpSession httpSession){
        User user = (User)httpSession.getAttribute("user");
        UserInformation userInformation;
        if (null == user.getUserInformation()) {
            userInformation = new UserInformation();
            userInformation.setUser(user);
        } else {
            userInformation = user.getUserInformation();
        }
        model.addAttribute("userInformation", userInformation);
        return "admin/userInformation/edit";
    }

    @PostMapping("edit")
    public String postEditUserImformation(@Validated UserInformation userInformation,
                                          BindingResult bindingResult,
                                          HttpSession httpSession,
                                          HttpServletRequest httpServletRequest,
                                          RedirectAttributes redirectAttributes){
        if (bindingResult.hasErrors()){
            return "admin/userInformation/edit";
        }
        User user = (User)httpSession.getAttribute("user");
        userInformation.setUser(user);
        userInformationRepository.saveAndFlush(userInformation);
        httpSession.setAttribute("user",userRepository.getOne(user.getId()));
        redirectAttributes.addFlashAttribute("message", Tools.getMessages(httpServletRequest,"saveSuccess","保存成功"));
        return "redirect:/admin/userInformation/edit";
    }

    @GetMapping("jobNumberValidate")
    @ResponseBody
    public boolean jobNumberValidate(Long id, Integer jobNumber) {
        return null == id ? !userInformationRepository.existsByJobNumber(jobNumber) : !userInformationRepository.existsByIdNotAndJobNumber(id, jobNumber);
    }

    @GetMapping("idCardValidate")
    @ResponseBody
    public boolean idCardValidate(Long id, String idCard) {
        return null == id ? !userInformationRepository.existsByIdCard(idCard) : !userInformationRepository.existsByIdNotAndIdCard(id, idCard);
    }

    @GetMapping("morningDateValidate")
    @ResponseBody
    public boolean morningDateValidate(Long id, @DateTimeFormat(pattern = "yyyy-MM-dd") Date morningDate) {
        return null == id ? !userInformationRepository.existsByMorningDate(morningDate) : !userInformationRepository.existsByIdNotAndMorningDate(id, morningDate);
    }
}
